package com.administrator.cloudapplication.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.administrator.cloudapplication.Interface.ListItemClickHelp;
import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.activitys.EbookDetailActivity;
import com.administrator.cloudapplication.activitys.MedioDetailActivity;
import com.administrator.cloudapplication.activitys.SearchMoreActivity;
import com.administrator.cloudapplication.adapter.VideoListAdapter;
import com.administrator.cloudapplication.base.BaseFragment;
import com.administrator.cloudapplication.callback.ReqCallBack;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.EbookModel;
import com.administrator.cloudapplication.models.MediaModel;
import com.administrator.cloudapplication.tools.GsonUtils;
import com.administrator.cloudapplication.tools.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 程序的第二个页面的组成页面,本页面主要做网络请求，并且获取数据显示到页面上。
 * 本页面的所有功能都可以参考NewsFragment页面。
 * Created by 祁晓峰 on 2017/4/17.
 */

public class VideoFragment extends BaseFragment implements ReqCallBack, SwipeRefreshLayout.OnRefreshListener, ListItemClickHelp {
    private ListView mListView;
    private View mView;
    ArrayList<MediaModel.DataBean> mDataBean = new ArrayList<>();
    ArrayList<EbookModel.EbookDataBean> mEbookDataBean = new ArrayList<>();
    ArrayList<MediaModel.DataBean.VideolistBean> mVideolistBean = new ArrayList<>();
    VideoListAdapter mMediaAdapter;
    SwipeRefreshLayout swipeRefreshLayout;
    int type;
    String url;


    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_video, container, false);
        swipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_video);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        mListView = (ListView) mView.findViewById(R.id.lv_video);
        return mView;
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        type = getArguments().getInt("type");
        switch (type) {
            case 0:
                url = Urls.VIDEO_LIST;
                break;
            case 1:
                url = Urls.AUDIO_LIST;
                break;
            case 2:
                url = Urls.BOOK_LIST;
                break;
        }
        requestWeb();
    }

    public void onRefresh() {
        requestWeb();

    }


    private void requestWeb() {
        showLoading();
        HttpUtils.getInstance(getActivity()).requestPostByAsyn2(url, null, this, 0);
    }

    public void onReqSuccess(String result, int flag) {
        stopLoading();
        swipeRefreshLayout.setRefreshing(false);
        switch (type) {
            case 0:
            case 1:
                mDataBean.clear();
                MediaModel mMediaModel = GsonUtils.json2Bean(result, MediaModel.class);
                mDataBean.addAll(mMediaModel.getData());
                mMediaAdapter = new VideoListAdapter(getActivity(), mDataBean, null, 0, this);
                break;
            case 2:
                mEbookDataBean.clear();
                EbookModel mEbookModel = GsonUtils.json2Bean(result, EbookModel.class);
                mEbookDataBean.addAll(mEbookModel.getData());
                mMediaAdapter = new VideoListAdapter(getActivity(), null, mEbookDataBean, 1, this);
                break;
        }

        mListView.setAdapter(mMediaAdapter);


    }


    @Override
    public void onReqFailed(String errorMsg, int flag) {
        showLoading();
    }


    public void onClick( int position) {
        Intent intent=new Intent(getActivity(), SearchMoreActivity.class);
        switch (type){
            case 0:case 1:
                intent.putExtra("cate",mDataBean.get(position).getCate());
                intent.putExtra("title_name",mDataBean.get(position).getCatename());
                break;
            case 2:
                intent.putExtra("cate",mEbookDataBean.get(position).getCate());
                intent.putExtra("title_name",mEbookDataBean.get(position).getCatename());
                break;
        }
        intent.putExtra("type",type+"");
        startActivity(intent);
    }



 @Override
    public void onImgClick(List<MediaModel.DataBean.VideolistBean> mVideolistBean,
                           List<EbookModel.EbookDataBean.BooklistBean> mEbookDataBean,int position) {
     Intent intent;
        switch (type){
            case 0:case 1:
                intent=new Intent(getActivity(), MedioDetailActivity.class);
                intent.putExtra("vid",mVideolistBean.get(position).getVid());
                intent.putExtra("title_name",mVideolistBean.get(position).getViname());
                intent.putExtra("type",type);
                startActivity(intent);
                break;
            case 2:
                intent=new Intent(getActivity(), EbookDetailActivity.class);
                intent.putExtra("id",mEbookDataBean.get(position).getBookid());
                intent.putExtra("author",mEbookDataBean.get(position).getAuthor());
                intent.putExtra("bookname",mEbookDataBean.get(position).getBookname());
                intent.putExtra("bookthumb",mEbookDataBean.get(position).getBookthumb());
                startActivity(intent);
                break;
        }


    }
}
